package com.kelvem.saas.workbanch.core.manager.controller;

import com.kelvem.saas.workbanch.core.base.Page;
import com.kelvem.saas.workbanch.core.exception.SaasAssert;
import com.kelvem.saas.workbanch.core.manager.model.BizPageDomEntity;
import com.kelvem.saas.workbanch.core.manager.request.BizPageDomQueryVo;
import com.kelvem.saas.workbanch.core.manager.service.BizPageDomService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 业务页面元素控制层
 *
 * @author kelvem
 * @version 1.0
 */
@RestController
@Slf4j
@RequestMapping("/bizPageDom")
public class BizPageDomController {

    @Resource
    private BizPageDomService bizPageDomService;
    
    /**
     * 增加业务页面元素
     * curl -X POST http://localhost:9090/bizPageDom/v1/add -H 'Content-Type: application/json' -d '{"id": null, "domUuid": "dom_uuid", "domType": "dom_type", "domSubType": "dom_sub_type", "parentDomUuid": "parent_dom_uuid", "bizPageId": 3, "metaTableId": 0, "metaTableColumnId": 3, "sortNo": 1, "panelType": "panel_type", "panelUuid": "panel_uuid", "actionJson": "action_json", "enName": "en_name", "chName": "ch_name", "controlType": "control_type", "dbType": "db_type", "defaultValue": "default_value", "optionValue": "option_value", "requireFlag": 0, "span": 9, "width": 5, "placeholder": "placeholder", "sortFlag": 1, "showFlag": 0, "editType": "edit_type", "propertyJson": "property_json", "bindDatasetName": "bind_dataset_name", "bindDatasetXpath": "bind_dataset_xpath", "sourceType": "source_type", "createUid": "create_uid", "updateUid": "update_uid", "memo": "memo"}'
     *
     * @param bizPageDom 业务页面元素信息
     * @return Long 主键id
     */
    @PostMapping("/v1/add")
    public Long add(@RequestBody(required = false) BizPageDomEntity bizPageDom) {
        SaasAssert.notNull(bizPageDom);

        log.debug("BizPageDomController:add:start, bizPageDom=【{}】", bizPageDom);
        Long bizPageDomId = bizPageDomService.addBizPageDom(bizPageDom);
        log.debug("BizPageDomController:add:end, bizPageDomId={}", bizPageDomId);

        return bizPageDomId;
    }

    /**
     * 修改业务页面元素
     * curl -X PUT http://localhost:9090/bizPageDom/v1/update -H 'Content-Type: application/json' -d '{"id": 10000, "domUuid": "dom_uuid", "domType": "dom_type", "domSubType": "dom_sub_type", "parentDomUuid": "parent_dom_uuid", "bizPageId": 3, "metaTableId": 0, "metaTableColumnId": 3, "sortNo": 1, "panelType": "panel_type", "panelUuid": "panel_uuid", "actionJson": "action_json", "enName": "en_name", "chName": "ch_name", "controlType": "control_type", "dbType": "db_type", "defaultValue": "default_value", "optionValue": "option_value", "requireFlag": 0, "span": 9, "width": 5, "placeholder": "placeholder", "sortFlag": 1, "showFlag": 0, "editType": "edit_type", "propertyJson": "property_json", "bindDatasetName": "bind_dataset_name", "bindDatasetXpath": "bind_dataset_xpath", "sourceType": "source_type", "createUid": "create_uid", "updateUid": "update_uid", "memo": "memo"}'
     *
     * @param bizPageDom 业务页面元素信息
     * @return Integer 修改记录数
     */
    @PostMapping("/v1/update")
    public Integer update(@RequestBody(required = false) BizPageDomEntity bizPageDom) {
        SaasAssert.notNull(bizPageDom);

        log.debug("BizPageDomController:update:start, bizPageDom=【{}】", bizPageDom);
        int count = bizPageDomService.updateBizPageDom(bizPageDom);
        log.debug("BizPageDomController:update:end, count={}", count);

        return count;
    }

    /**
     * 删除业务页面元素
     * curl -X DELETE 'http://localhost:9090/bizPageDom/v1/delete/10002'
     *
     * @param bizPageDomId 业务页面元素id
     * @return Integer 删除记录数
     */
    @PostMapping("/v1/delete/{bizPageDomId}")
    public Integer delete(@PathVariable(required = false) Long bizPageDomId) {
        SaasAssert.notNull(bizPageDomId);

        log.debug("BizPageDomController:delete:start, bizPageDomId={}", bizPageDomId);
        int count = bizPageDomService.deleteBizPageDom(bizPageDomId);
        log.debug("BizPageDomController:delete:end, count={}", count);

        return count;
    }

    /**
     * 查询业务页面元素分页数据
     * curl -X POST http://localhost:9090/bizPageDom/v1/queryPage -H 'Content-Type: application/json' -d '{"id": 10000}'
     *
     * @param bizPageDomQueryVo 查询用
     * @return Page<BizPageDomEntity> 业务页面元素分页数据
     */
    @PostMapping("/v1/queryPage")
    public Page<BizPageDomEntity> queryPage(@RequestBody(required = false) BizPageDomQueryVo bizPageDomQueryVo) {
        SaasAssert.notNull(bizPageDomQueryVo);

        log.debug("BizPageDomController:queryPage:start, bizPageDomQueryVo=【{}】", bizPageDomQueryVo);
        Page<BizPageDomEntity> page = bizPageDomService.queryBizPageDomPage(bizPageDomQueryVo);
        log.debug("BizPageDomController:queryPage:end, page.count={}", page.getSize());

        return page;
    }

    /**
     * 根据id查业务页面元素信息
     * curl -X GET 'http://localhost:9090/bizPageDom/v1/get/10000'
     *
     * @param bizPageDomId 业务页面元素id
     * @return BizPageDom 业务页面元素信息
     */
    @GetMapping("/v1/get/{bizPageDomId}")
    public BizPageDomEntity getById(@PathVariable(required = false) Long bizPageDomId) {
        SaasAssert.notNull(bizPageDomId);

        log.debug("BizPageDomController:getById:start, bizPageDomId={}", bizPageDomId);
        BizPageDomEntity bizPageDom = bizPageDomService.getBizPageDomById(bizPageDomId);
        log.debug("BizPageDomController:getById:end, bizPageDom=【{}】", bizPageDom);

        return bizPageDom;
    }
    
}
